//
//  GSLocalAuthencationView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/6.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "GSLocalAuthencationView.h"

#import "GSLocalAuthUserInfoView.h"

@interface GSLocalAuthencationViewButton ()

/**
 *authIV
 */
@property (nonatomic, strong) UIImageView *authIV;

/**
 *titleLb
 */
@property (nonatomic, strong) QMUILabel *titleLb;

@end

@implementation GSLocalAuthencationViewButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.authIV = [UIImageView new];
    self.authIV.contentMode = UIViewContentModeScaleAspectFit;
    self.authIV.tintColor = GSLOGIN_THEME_COLOR;
    [self addSubview:self.authIV];
    [self.authIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo([self.class authIVSize]);
    }];
    //
    self.titleLb = [QMUILabel new];
    self.titleLb.textColor = GSLOGINRGBRGBHEX(0x666666);
    self.titleLb.font = [self.class titleFont];
    self.titleLb.numberOfLines = 2;
    self.titleLb.textAlignment = NSTextAlignmentCenter;
    self.titleLb.adjustsFontSizeToFitWidth = true;
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.authIV.mas_bottom).offset(10.0);
        make.left.right.mas_equalTo(self);
        make.height.mas_lessThanOrEqualTo([self.class titleHeight]);
    }];
}


+ (CGSize)authIVSize {
    return CGSizeMake(77.0, 77.0);
}
+ (UIFont *)titleFont {
    return [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
}
+ (CGFloat)titleHeight {
    return [self titleFont].lineHeight*2;
}

+ (CGFloat)defaultHeight {
    return [self authIVSize].height+10.0+[self titleHeight];
}

@end

@interface GSLocalAuthencationView ()

@property (nonatomic, strong) UIView *sepLine;


/**
 *显示当前指纹或面容登录用户信息
 */
@property (nonatomic, strong) GSLocalAuthUserInfoView *userInfoView;


@end

@implementation GSLocalAuthencationView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        
        //设置 指纹登录或者面容登录
        DDLABiometryType type = [LocalAuthenticationManager sharedLocalAuth].biometryType;
        switch (type) {
            case DDLABiometryTypeFaceID:
            {
                self.authButton.authIV.image = [LOAD_IMAGE(@"bc_face") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
                self.authButton.titleLb.text = GSLoginLocalized(@"Click to face login");
            }
                break;
            case DDLABiometryTypeTouchID:
            {
                self.authButton.authIV.image = [LOAD_IMAGE(@"bc_fingerprint") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
                self.authButton.titleLb.text = GSLoginLocalized(@"Click to fingerprint login");
            }
            default:
                break;
        }
    }
    return self;
}

- (void)setupSubviews {
    self.authButton = [GSLocalAuthencationViewButton new];
    [self addSubview:self.authButton];
    [self.authButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_centerY).offset(20.0);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(200.0);
        make.height.mas_equalTo([GSLocalAuthencationViewButton defaultHeight]);
    }];
    //
    self.userInfoView = [GSLocalAuthUserInfoView new];
    [self addSubview:self.userInfoView];
    [self.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.authButton.mas_top);
    }];
    
    self.sepLine = [UIView new];
    self.sepLine.backgroundColor = GSLOGINRGBRGBHEX(0xF3F3F3);
    [self addSubview:self.sepLine];
    [self.sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_centerY).offset(46.0);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(200.0);
    }];
    self.moreButton = [QMUIButton new];
    [self.moreButton setTitleColor:GSLOGIN_THEME_COLOR forState:UIControlStateNormal];
    [self.moreButton setTitle:GSLoginLocalized(@"More") forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightRegular];
    [self addSubview:self.moreButton];
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sepLine.mas_bottom).offset(10.0);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self.sepLine);
        make.height.mas_equalTo(50.0);
    }];
}

- (void)setUserInfo:(UserInfoModel *)userInfo {
    _userInfo = userInfo;
    //
    self.userInfoView.userInfo = userInfo;
}

@end
